import matplotlib.pyplot as plt
import numpy as np

# 设置中文字体
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用黑体
plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题

# 定义地块类型和作物名称
land_types = ['Plain Dry Land', 'Terraced Fields', 'Hillside Land', 'Irrigated Land', 'Ordinary Greenhouse', 'Smart Greenhouse']
crops = ['Soya beans', 'Black bean', 'Ormosia bean', 'Mung bean', 'Crawling bean', 'Wheat', 'Corn', 'Millet', 'Sorghum', 'Broomcorn millet', 'Buckwheat',
         'Pumpkin', 'Sweet potato', 'Naked oats', 'Barley', 'Rice', 'Cowpea', 'Canavalia', 'Kidney bean', 'Potato', 'Tomato', 'Eggplant',
         'Spinach', 'Green pepper', 'Cauliflower', 'Cabbage', 'Romaine lettuce', 'Brassica chinensis', 'Cucumber', 'Lettuce', 'Pepper', 'Water spinach',
         'Yellow Heart Cabbage', 'Celery', 'Chinese cabbage', 'White radish', 'Carrot', 'Elm mushroom', 'Mushroom', 'Pleurotus pleurotus', 'Morel']

# 初始化一个二维矩阵，表示每个地块与作物是否可以种植
matrix = np.zeros((len(land_types), len(crops)))

# 平旱地可以种植的作物
land1_crops = ['Soya beans', 'Black bean', 'Ormosia bean', 'Mung bean', 'Crawling bean', 'Wheat', 'Corn', 'Millet', 'Sorghum', 'Broomcorn millet', 'Buckwheat',
               'Pumpkin', 'Sweet potato', 'Naked oats', 'Barley']
# 梯田可以种植的作物
land2_crops = ['Soya beans', 'Black bean', 'Ormosia bean', 'Mung bean', 'Crawling bean', 'Wheat', 'Corn', 'Millet', 'Sorghum', 'Broomcorn millet', 'Buckwheat',
               'Pumpkin', 'Sweet potato', 'Naked oats', 'Barley']
# 山坡地可以种植的作物
land3_crops = ['Soya beans', 'Black bean', 'Ormosia bean', 'Mung bean', 'Crawling bean', 'Wheat', 'Corn', 'Millet', 'Sorghum', 'Broomcorn millet', 'Buckwheat',
               'Pumpkin', 'Sweet potato', 'Naked oats', 'Barley']
# 水浇地可以种植的作物
land4_crops = ['Rice', 'Cowpea', 'Canavalia', 'Kidney bean', 'Potato', 'Tomato', 'Eggplant', 'Spinach', 'Green pepper', 'Cauliflower', 'Cabbage',
               'Romaine lettuce', 'Brassica chinensis', 'Cucumber', 'Lettuce', 'Pepper', 'Water spinach', 'Yellow Heart Cabbage', 'Celery', 'Chinese cabbage',
               'White radish', 'Carrot']
# 普通大棚可以种植的作物
land5_crops = ['Cowpea', 'Canavalia', 'Kidney bean', 'Potato', 'Tomato', 'Eggplant', 'Spinach', 'Green pepper', 'Cauliflower', 'Cabbage', 'Romaine lettuce',
               'Brassica chinensis', 'Cucumber', 'Lettuce', 'Pepper', 'Water spinach', 'Yellow Heart Cabbage', 'Celery', 'Elm mushroom', 'Mushroom',
               'Pleurotus pleurotus', 'Morel']
# 智慧大棚可以种植的作物
land6_crops = ['Cowpea', 'Canavalia', 'Kidney bean', 'Potato', 'Tomato', 'Eggplant', 'Spinach', 'Green pepper', 'Cauliflower', 'Cabbage', 'Romaine lettuce',
               'Brassica chinensis', 'Cucumber', 'Lettuce', 'Pepper', 'Water spinach', 'Yellow Heart Cabbage', 'Celery']

# 填充矩阵，根据地块类型和作物情况
def fill_matrix(land_crops, land_index):
    for crop in land_crops:
        crop_index = crops.index(crop)
        matrix[land_index][crop_index] = 1

# 填充每个地块的种植情况
fill_matrix(land1_crops, 0)  # 平旱地
fill_matrix(land2_crops, 1)  # 梯田
fill_matrix(land3_crops, 2)  # 山坡地
fill_matrix(land4_crops, 3)  # 水浇地
fill_matrix(land5_crops, 4)  # 普通大棚
fill_matrix(land6_crops, 5)  # 智慧大棚

# 绘制热图
plt.figure(figsize=(10, 8))

# 使用热图展示矩阵，1为绿色，0为白色
cax = plt.imshow(matrix, cmap='Greens', aspect='auto')

# 设置坐标轴标签
plt.xticks(np.arange(len(crops)), crops, rotation=90, fontsize=8)
plt.yticks(np.arange(len(land_types)), land_types)

# 自定义色条，显示0和1
cbar = plt.colorbar(cax, ticks=[0, 1])
cbar.set_label("Plantable condition")
cbar.set_ticks([0, 1])  # 只显示0和1
cbar.ax.set_yticklabels(['Non-plantable', 'Plantable'])  # 设置对应标签

# 添加标题
plt.title("Cultivation of different plots and crops")

# 在右上角添加面积信息
area_info = """ 
Plain Dry Land: 365 acres
Terraced Fields: 619 acres
Hillside Land: 108 acres
Irrigated Land: 109 acres
Ordinary Greenhouse: 9.6 acres
Smart Greenhouse: 2.4 acres
"""
plt.text(1.15, 0.95, area_info, transform=plt.gca().transAxes, fontsize=10,
         verticalalignment='top', horizontalalignment='left', bbox=dict(facecolor='white', edgecolor='none', alpha=0.7))

# 显示图像
plt.tight_layout()  # 自动调整布局，避免标签重叠

plt.savefig("可种植情况.png", bbox_inches='tight', dpi=300)  # 设置 dpi 为 300，图片分辨率更高

plt.show()
